<template>
  <div class="assess">
    <Steps :active-step="2" />
    <c-table-box ref="cTableBox" class="table-span" hide-func :table-span-method="spanMethod" :data="tableData" :table-config="tableConfig" />
    <!-- <div style="display:flex;margin-top:20px">
      <h3 style="width:45px">备注：</h3>
      <el-input v-model="remark" type="textarea" maxlength="1000" show-word-limit />
    </div> -->

    <div class="btn-container">
      <c-button type="primary" @click="clickPrevStep">上一步</c-button>
      <c-button type="primary" @click="saveHandle">保存</c-button>
      <c-button type="primary" @click="clickConfirmHandle">确认</c-button>
    </div>
  </div>
</template>
<script>
import Steps from './components/steps'
import { postConfigScoreCriteriaPage } from '@/api/decorate'
import { postAcceptAssess, postCommitAcceptAssess, postConstUnitAssessScoreDetail } from '@/api/check'
export default {
  name: 'CheckHardExecuteAssess',
  components: {
    Steps
  },
  data() {
    return {
      tableData: [],
      remark: '', // 备注
      tableConfig: {
        setting: [
          {
            prop: 'scoreType',
            label: '评估类别'
          },
          {
            prop: 'totalScore',
            label: '总分值'
          },
          {
            prop: 'scoreDesc',
            label: '评估说明'
          },
          {
            prop: 'detailItem',
            label: '考核明细'
          },
          {
            prop: 'grading',
            label: '评分标准'
          },
          {
            prop: 'scoreDescItem',
            label: '评分说明',
            render: (h, row) => {
              return h('el-input', {
                props: {
                  placeholder: '请输入评分说明',
                  clearable: true,
                  value: row.scoreDescItem
                },
                on: {
                  input: v => {
                    this.$set(row, 'scoreDescItem', v)
                  }
                }
              })
            }
          },
          {
            prop: 'acceptGroupScore',
            label: '验收小组评分',
            render: (h, row) => {
              return h('c-input-number', {
                props: {
                  placeholder: '验收小组评分',
                  clearable: true,
                  value: row.acceptGroupScore,
                  float: true
                },
                on: {
                  input: v => {
                    this.$set(row, 'acceptGroupScore', v)
                  },
                  change: (v) => {
                    if (row.totalScore < v) {
                      this.$message.warning('不能大于总分值！')
                      this.$set(row, 'acceptGroupScore', 0)
                      return
                    }
                    this.assignmentHandler(row.id, 'acceptGroupScore', v)
                  }
                }
              })
            }
          },
          {
            prop: 'supervisorScore',
            label: '工程监理评分',
            render: (h, row) => {
              return h('c-input-number', {
                props: {
                  placeholder: '工程监理评分',
                  clearable: true,
                  value: row.supervisorScore,
                  float: true
                },
                on: {
                  input: v => {
                    this.$set(row, 'supervisorScore', v)
                  },
                  change: (v) => {
                    if (row.totalScore < v) {
                      this.$message.warning('不能大于总分值！')
                      this.$set(row, 'supervisorScore', 0)
                      return
                    }
                    this.assignmentHandler(row.id, 'supervisorScore', v)
                  }
                }
              })
            }
          },
          {
            prop: 'customerScore',
            label: '经销商评分',
            render: (h, row) => {
              return h('c-input-number', {
                props: {
                  placeholder: '经销商评分',
                  clearable: true,
                  value: row.customerScore,
                  float: true
                },
                on: {
                  input: v => {
                    this.$set(row, 'customerScore', v)
                  },
                  change: (v) => {
                    if (row.totalScore < v) {
                      this.$message.warning('不能大于总分值！')
                      this.$set(row, 'customerScore', 0)
                      return
                    }
                    this.assignmentHandler(row.id, 'customerScore', v)
                  }
                }
              })
            }
          },
          {
            prop: 'actualScore',
            label: '最终平均得分'
          }
        ]
      }
    }
  },
  computed: {
  },
  created() {
    this.getScoreDetail()
  },
  methods: {
    // 赋值
    assignmentHandler(id, key, v) {
      this.tableData.forEach(e => {
        if (e.id === id) {
          this.$set(e, key, v)
          const actualScore = ((parseFloat(e.customerScore || 0) + parseFloat(e.supervisorScore || 0) + parseFloat(e.acceptGroupScore || 0)) / 3).toFixed(2)
          this.$set(e, 'actualScore', actualScore)
        }
      })
    },
    handlerData(data) {
      const d = []
      for (const item of data) {
        const isAss = !!item.assessDetailsDtos
        const ca = item.configScoreDetailDTOS || item.assessDetailsDtos || []
        // const flagId = Math.floor(Math.random()*0xffffff).toString(16)
        for (let i = 0; i < ca.length; i++) {
          const score = ca[i]
          let v = {}
          if (isAss) {
            v = {
              scoreNature: item.scoreNature, // 考核性质：日常考核、项目过程考核、项目验收考核
              scoreType: item.scoreType, // 考核类别
              totalScore: item.totalScore, // 总分值
              scoreDesc: item.scoreDesc, // 考核说明
              id: item.id, // 评分标准ID

              calculation: score.calculation, // 计算方式
              detailItem: score.detailItem, // 明细项目
              grading: score.grading, // 评分标准
              scoreDetailId: score.scoreDetailId, // 考核明细配置ID
              scoreCriteriaId: item.scoreCriteriaId, // 评分标准ID
              scoreDescItem: score.scoreDesc, // 评分说明
              actualScore: item.actualScore || 0,
              customerScore: item.customerScore || 0,
              supervisorScore: item.supervisorScore || 0,
              acceptGroupScore: item.acceptGroupScore || 0,
              spanRow: 0
            }
          } else {
            v = {
              scoreNature: item.scoreNature, // 考核性质：日常考核、项目过程考核、项目验收考核
              scoreType: item.scoreType, // 考核类别
              totalScore: item.totalScore, // 总分值
              scoreDesc: item.scoreDesc, // 考核说明
              id: item.id,

              calculation: score.calculation, // 计算方式
              detailItem: score.detailItem, // 明细项目
              grading: score.grading, // 评分标准
              scoreDetailId: score.id,
              scoreCriteriaId: score.scoreCriteriaId, // 评分标准ID
              scoreDescItem: score.scoreDesc, // 评分说明
              actualScore: item.actualScore || 0,
              customerScore: item.customerScore || 0,
              supervisorScore: item.supervisorScore || 0,
              acceptGroupScore: item.acceptGroupScore || 0,
              spanRow: 0
            }
          }
          if (i === 0) {
            v.spanRow = ca.length
          }
          d.push(v)
        }
      }
      return d
    },
    getScoreDetail() {
      const { decorationTaskCode, unitCode } = this.$route.query
      postConstUnitAssessScoreDetail('PRO_CHECK', unitCode, decorationTaskCode).then(res => {
        if (!res.data || res.data.length === 0) {
          postConfigScoreCriteriaPage('PRO_CHECK').then(res => {
            this.tableData = this.handlerData(res.data.records)
          })
        } else {
          this.tableData = this.handlerData(res.data)
        }
      })
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      const indexs = [0, 1, 2, 3, 7, 8, 9, 10]
      if (indexs.includes(columnIndex)) {
        return {
          rowspan: row.spanRow,
          colspan: 1
        }
      }
    },
    genData() {
      const t = {}
      this.tableData.forEach(e => {
        if (t[e.id]) {
          t[e.id].push(e)
        } else {
          t[e.id] = []
          t[e.id].push(e)
        }
      })
      // acceptGroupScore	验收小组评分
      // actualScore	实际得分
      // constUnitAssessDetailDTOS
      // customerScore	经销商评分
      // decorationTaskCode	装修任务号
      // scoreCriteriaId	评分类别配置ID
      // supervisorScore	工程监理评分
      // unitCode	施工单位编码
      const data = []
      // eslint-disable-next-line no-unused-vars
      for (const [id, vs] of Object.entries(t)) {
        const d = {
          unitCode: this.$route.query.constUnitCode,
          decorationTaskCode: this.$route.query.decorationTaskCode,
          constUnitAssessDetailDTOS: []
        }
        for (const v of vs) {
          d.acceptGroupScore = v.acceptGroupScore
          d.actualScore = v.actualScore
          d.customerScore = v.customerScore
          d.supervisorScore = v.supervisorScore
          d.scoreCriteriaId = v.scoreCriteriaId
          d.unitCode = this.$route.query.unitCode
          d.constUnitAssessDetailDTOS.push({
            scoreDetailId: v.scoreDetailId,
            scoreDesc: v.scoreDescItem,
            detailItem: v.detailItem,
            grading: v.grading,
            calculation: v.calculation
          })
        }
        data.push(d)
      }
      // console.log(data)
      return data
    },
    saveHandle() {
      const data = this.genData()
      return postAcceptAssess(data, this.$route.query.id).then(res => {
        this.$message.success('操作成功！')
        this.$store.dispatch('views/goFirst')
      })
    },
    clickPrevStep() {
      this.$store.dispatch('views/goBack')
    },
    clickConfirmHandle() {
      const data = this.genData()
      return postCommitAcceptAssess(data, this.$route.query.id).then(res => {
        this.$message.success('操作成功！')
        this.$store.dispatch('views/goFirst')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.assess{
  padding-top: 20px;
}
</style>
